package com.wdc.service.impl;


import com.wdc.entity.dto.UserContactSearchResultDto;
import com.wdc.entity.po.GroupInfo;
import com.wdc.entity.po.UserInfo;
import com.wdc.entity.query.GroupInfoQuery;
import com.wdc.entity.query.SimplePage;
import com.wdc.entity.query.UserInfoQuery;
import com.wdc.enums.PageSize;
import com.wdc.entity.vo.PaginationResultVo;
import com.wdc.entity.po.UserContact;
import com.wdc.entity.query.UserContactQuery;
import com.wdc.enums.UserContactStatusEnum;
import com.wdc.enums.UserContactTypeEnum;
import com.wdc.mapper.GroupInfoMappers;
import com.wdc.mapper.UserContactMappers;
import com.wdc.mapper.UserInfoMappers;
import com.wdc.service.UserContactService;
import com.wdc.utils.CopyTools;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;

/**
 * @author: WDC
 * @description:Service
 * @date:2024/09/19
 */
@Service("userContactService")
public class UserContactServiceImpl implements UserContactService {


    @Resource
    private UserContactMappers<UserContact, UserContactQuery> userContactMappers;

    @Resource
    private UserInfoMappers<UserInfo, UserInfoQuery> userInfoMappers;

    @Resource
    private GroupInfoMappers<GroupInfo, GroupInfoQuery> groupInfoMappers;

    /**
     * 根据条件查询列表
     */
    public List<UserContact> findListByParam(UserContactQuery query) {
        return this.userContactMappers.selectList(query);

    }

    /**
     * 根据条件查询数量
     */
    public Integer findCountByParam(UserContactQuery query) {
        return this.userContactMappers.selectCount(query);
    }

    /**
     * 分页查询
     */
    public PaginationResultVo<UserContact> findListByPage(UserContactQuery query) {
        Integer count = this.findCountByParam(query);
        Integer pageSize = query.getPageSize() == null ? PageSize.SIZE15.getSize() : query.getPageSize();
        SimplePage page = new SimplePage(query.getPageNo(), count, pageSize);
        query.setSimplePage(page);
        List<UserContact> list = this.findListByParam(query);
        PaginationResultVo<UserContact> result = new PaginationResultVo(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
        return result;
    }

    /**
     * 新增
     */
    public Integer add(UserContact bean) {
        return this.userContactMappers.insert(bean);
    }

    /**
     * 批量新增
     */
    public Integer addBatch(List<UserContact> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.userContactMappers.insertBatch(listBean);
    }

    /**
     * 批量新增或修改
     */
    public Integer addOrUpdateBatch(List<UserContact> listBean) {
        if (listBean == null || listBean.isEmpty()) {
            return 0;
        }
        return this.userContactMappers.insertOrUpdateBatch(listBean);
    }

    /**
     * 根据UserIdAndContactId查询
     */
    public UserContact getUserContactByUserIdAndContactId(String userId, String contactId) {
        return this.userContactMappers.selectByUserIdAndContactId(userId, contactId);
    }

    /**
     * 根据UserIdAndContactId更新
     */
    public Integer updateUserContactByUserIdAndContactId(UserContact bean, String userId, String contactId) {
        return this.userContactMappers.updateByUserIdAndContactId(bean, userId, contactId);
    }

    /**
     * 根据UserIdAndContactId删除
     */
    public Integer deleteUserContactByUserIdAndContactId(String userId, String contactId) {
        return this.userContactMappers.deleteByUserIdAndContactId(userId, contactId);
    }

    @Override
    public UserContactSearchResultDto searchContact(String userId, String contactId) {
        UserContactTypeEnum typeEnum = UserContactTypeEnum.getByPrefix(contactId);
        if (typeEnum == null) {
            return null;
        }
        UserContactSearchResultDto resultDto = new UserContactSearchResultDto();
        switch (typeEnum) {
            case USER:
                UserInfo userInfo = userInfoMappers.selectByUserId(contactId);
                if (userInfo == null) {
                    return null;
                }
                resultDto = CopyTools.copy(userInfo, UserContactSearchResultDto.class);
                break;
            case GROUP:
                GroupInfo groupInfo = groupInfoMappers.selectByGroupId(contactId);
                if (groupInfo == null) {
                    return null;
                }
                resultDto.setNickName(groupInfo.getGroupName());
                break;
        }
        resultDto.setContactType(typeEnum.toString());
        resultDto.setContactId(contactId);

        if (userId.equals(contactId)) {
            resultDto.setStatus(UserContactStatusEnum.FRIEND.getStatus());
        }
        //查询是否是好友
        UserContact userContact = this.userContactMappers.selectByUserIdAndContactId(userId, contactId);
        resultDto.setStatus(userContact == null ? null : userContact.getStatus());


        return resultDto;
    }


}